from PySide6.QtWidgets import QMainWindow, QFileDialog, QMessageBox, QTableWidgetItem, QHeaderView

# from logic.get_sample_data_from_sizingTable import get_sample_genotype, get_sample_data
from logic.get_sample_data_from_sizingTable import *
from ui.window import Ui_MainWindow


class MixtureAnalysis(QMainWindow):
    def __init__(self):
        super().__init__()
        self.sample1st_genotype = {}
        self.sample2nd_genotype = {}
        self.samples_data = {}
        self.results = []

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.connectSignals()

    def connectSignals(self):
        self.ui.uploadGenoButton.clicked.connect(self.uploadGeno)
        self.ui.uploadSizingTableButton.clicked.connect(self.uploadSizingTable)
        self.ui.analysisButton.clicked.connect(self.analysis)

    def uploadGeno(self):
        path = QFileDialog.getOpenFileName(self,"上传样本分型表","","Excel Files (*.xlsx)")
        if path != ('',''):
            longFilePath = path[0]
            self.sample1st_genotype, self.sample2nd_genotype = get_sample_genotype(longFilePath)

    def uploadSizingTable(self):
        path = QFileDialog.getOpenFileName(self, "上传样本分型表", "", "Text Files (*.txt);;All files (*.*)")
        if path != ('', ''):
            longFilePath = path[0]
            self.sample_data = get_sample_data(longFilePath)

    def analysis(self):
        self.results = get_ratio_samples(self.sample_data, self.sample1st_genotype, self.sample2nd_genotype)
        self.viewTable()

    def viewTable(self):
        self.ui.resultTable.setRowCount(len(self.results))
        self.ui.resultTable.setColumnCount(5)
        self.ui.resultTable.setHorizontalHeaderLabels(['样本号','基因座','样本1峰高','样本2峰高','比值（样本1/样本2）'])
        self.ui.resultTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        for i in range(self.ui.resultTable.rowCount()):
            for j in range(self.ui.resultTable.columnCount()):
                self.ui.resultTable.setItem(i, j, QTableWidgetItem(str(self.results[i][j])))





